<template>
  <div id="app">
    <h1>使用useState方法通过数组拿到state数据</h1>
    <h2>counter:{{counter}}</h2>
    <h2>name:{{name}}</h2>
    <h2>age:{{age}}</h2>
    <hr>
    <h1>传统方法</h1>
    <button @click="sub">-1</button>
    <span>{{counter}}</span>
    <button @click="add">+1</button>
    <hr>
    <h1>使用mapMutations</h1>
    <button @click="decrement">-1</button>
    <span>{{counter}}</span>
    <button @click="increment">+1</button>
    <hr>
    <h1>使用mapMutations</h1>
    <button @click="decrement_n(10)">-10</button>
    <span>{{counter}}</span>
    <button @click="increment_n(10)">+10</button>
  </div>
</template>

<script>
import { useState } from "../hooks/useState";
import { useStore, mapMutations } from "vuex";
export default {
  setup() {
    // 使用mapState封装的函数拿到state
    const storeState1 = useState(["counter", "name", "age"]);
    // 拿到store对象
    const store = useStore();
    // 传统方法操作mutations
    // 点击+1
    const add = () => {
      store.commit("increment");
    };
    // 点击-1
    const sub = () => {
      store.commit("decrement");
    };

    // 使用mapMutations
    const storeMutations = mapMutations([
      "increment",
      "increment_n",
      "decrement",
      "decrement_n",
    ]);

    return {
      ...storeState1,
      add,
      sub,
      ...storeMutations,
    };
  },
};
</script>

<style lang="less">
#app {
  text-align: center;
}
</style>
